package com.fjh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fjh.domain.Basket;
import com.fjh.service.BasketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("p/shopCart")
@Api(tags = "购物车管理")
public class BasketContrller {

    @Autowired
    private BasketService basketService;

    @GetMapping("prodCount")
    @ApiOperation("查询用户的购物车商品数量")
    public ResponseEntity<Integer> getBasketCount(){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        Integer count = basketService.getCount(userId);
        return ResponseEntity.ok(count);
    }

    @PostMapping("changeItem")
    @ApiOperation("修改购物车中的商品数量")
    public ResponseEntity<Void> changeItem(@RequestBody Basket basket){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        basket.setUserId(userId);
        basketService.changeItem(basket);
        return ResponseEntity.ok().build();
    }






    //-------------------------------------

    /**
     * 远程调用购物车模块  根据ids拿到购物车集合
     * @param basketIds
     * @return
     */
    @PostMapping("getBasketListByIds")
    @ApiOperation("根据ids拿到购物车集合")
    List<Basket> getBasketListByIds(@RequestBody List<Long> basketIds){
        return basketService.listByIds(basketIds);
    }

    @PostMapping("clearCart")
    @ApiOperation("根据skuId和user清空购物车")
    void clearCart(@RequestBody List<Long> allSkuIds,@RequestParam String userId){
        basketService.remove(new LambdaQueryWrapper<Basket>()
                .eq(Basket::getUserId,userId)
                .in(Basket::getSkuId,allSkuIds)
        );

    }
}
